﻿using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using GC_FFW.Common.Abstractions.Interfaces.Helpers;

namespace GC_FFW.Common.Helpers
{
	public class XmlSerializerService : IXmlSerializerService
	{
		public void SerializeToXML<T>(T documentScheme, string documentPath)
		{
			var serializer = new XmlSerializer(typeof(T));

			using (var textWriter = new StreamWriter(documentPath))
			{
				serializer.Serialize(textWriter, documentScheme);
			}
		}

		public IList<T> DeserializeParams<T>(string documentPath)
		{
			var serializer = new XmlSerializer(typeof(List<T>));

			List<T> result;

			using (var reader = XmlReader.Create(documentPath))
			{
				result = (List<T>)serializer.Deserialize(reader);
			}

			return result;
		}

		public T Deserialize<T>(string documentPath)
		{
			var serializer = new XmlSerializer(typeof(T));

			T obj;

			using (var reader = XmlReader.Create(documentPath))
			{
				obj = (T)serializer.Deserialize(reader);
			}

			return obj;
		}
	}
}
